#Region " Imports "

Imports System.Data
Imports System.Data.SqlClient
Imports BlazeApps.Library.News

#End Region

Partial Class Usercontrols_News_AddNews
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected Property NewsGuid() As Guid
        Get
            Return GetGuidFromVS("NewsGuid")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("NewsGuid", value)
        End Set
    End Property

    Protected Property NewButtonClicked() As Boolean
        Get
            Return GetBooleanFromVS("NewButtonClicked")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("NewButtonClicked", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Add News")

        GetQueryStringValues()
        ConfigBaseEditorCtrls()

        If IsUserAuthenticated Then
            If Me.Page.IsPostBack = False Then

				BuildFCKEditor(EditorPageType.NewsEditor)

                If Not (NewsGuid = Guid.Empty) Then
                    LoadNews()
                End If
            End If
        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveNews()
        Dim NewsItem As NewsDetail = NewsDetail.NewNewsDetail

        NewsItem.title = Me.uxNewsTitleTextBox.Text
		NewsItem.body = Me.uxNewsTextbox.Value
		NewsItem.expiredateString = Me.uxExpirationDateTextBox.Text
		NewsItem.Sticky = Me.uxStickyCheckBox.Checked
		NewsItem.Userid = CurrentUserID
		NewsItem.Save()

	End Sub

	Private Sub UpdateNews()
		Dim NewsItem As NewsDetail = NewsDetail.GetNewsDetail(NewsGuid)

		NewsItem.title = Me.uxNewsTitleTextBox.Text
		NewsItem.body = Me.uxNewsTextbox.Value
		NewsItem.expiredateString = Me.uxExpirationDateTextBox.Text
		NewsItem.Sticky = Me.uxStickyCheckBox.Checked
		NewsItem.Save()
	End Sub

#End Region

#Region " Load Data "

	Private Sub LoadNews()

		NewsGuid = ConvertToGuid(NewsGuid)

		Dim NewsItem As NewsDetail = NewsDetail.GetNewsDetail(NewsGuid)

		Me.uxNewsTextbox.Value = NewsItem.body
		Me.uxNewsTitleTextBox.Text = NewsItem.title
		Me.uxExpirationDateTextBox.Text = NewsItem.expiredateString
		Me.uxStickyCheckBox.Checked = NewsItem.Sticky

	End Sub

#End Region

#Region " Form Controls Events "

	Protected Sub uxNewNewsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxNewNewsButton.Click
		'Response.Redirect(String.Format("{0}.aspx?pid=news_addnews", AppPage))

		NewButtonClicked = True

		NewsGuid = Guid.Empty

		Me.uxNewsTextbox.Value = String.Empty
		Me.uxNewsTitleTextBox.Text = String.Empty
		Me.uxExpirationDateTextBox.Text = String.Empty
		Me.uxStickyCheckBox.Checked = False

	End Sub

	Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
		If NewsGuid = Guid.Empty Then
			SaveNews()

			If EnableEmailNotifications Then
				If Me.uxNewsLetterCheckBox.Checked Then
					SendNewsLetter()
				End If
			End If

		Else
			UpdateNews()
		End If

		Response.Redirect(String.Format("{0}.aspx", AppPage))

	End Sub

	Protected Sub uxDeleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxDeleteButton.Click
		NewsDetail.DeleteNewsDetail(NewsGuid)

		Response.Redirect(String.Format("{0}.aspx", AppPage))

	End Sub

	Protected Sub uxResetButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetButton.Click
		If Not (NewsGuid = Guid.Empty) Then
			LoadNews()
		Else
			Me.uxNewsTextbox.Value = String.Empty
			Me.uxNewsTitleTextBox.Text = String.Empty
			Me.uxExpirationDateTextBox.Text = String.Empty
			Me.uxStickyCheckBox.Checked = False
		End If
	End Sub

#End Region

#Region " Helpers and Methods "

	Protected Sub GetQueryStringValues()
		If NewButtonClicked = False Then
			NewsGuid = GetGuidFromQueryString("id")
		End If
	End Sub

	Private Sub ConfigBaseEditorCtrls()
		Me.FCKEditor = Me.uxNewsTextbox
	End Sub

	Private Sub SendNewsLetter()
		Dim DT As New DataTable
		Dim DR As DataRow
		Dim PU As New ProfileUtility
		Dim MU As New MembershipUtility

		DT = MU.GetAllUsers.Tables(0)

		For Each DR In DT.Rows

			If PU.ReceiveNewsLetter(Membership.GetUser(UserID(DR("username"))).UserName.ToString) Then

				Dim ToAddress As String = Membership.GetUser(UserID(DR("username"))).Email
				Dim FromAddress As String = MailSettingsSection.Smtp.From.ToString().Trim()
				Dim EmailBody As New StringBuilder
				Dim Subject As String = String.Format("{0} - {1}", SiteTitle, Resources.Resource.NewsLetter)

				EmailBody.Append("<br>")
				EmailBody.Append(String.Format("<b>{0}</b>", Me.uxNewsTitleTextBox.Text))
				EmailBody.Append("<br>")
				EmailBody.Append(Me.uxNewsTextbox.Value)
				EmailBody.Append("<br>")
				EmailBody.Append("<br>")
				EmailBody.Append(String.Format("<a href=""http://{0}{1}"">{2}</a>", Request.ServerVariables("HTTP_HOST"), Request.ServerVariables("URL"), SiteTitle))
				EmailBody.Append("<br>")
				EmailBody.Append("<br>")
				EmailBody.Append(String.Format("<a href=""http://{0}{1}?pid=cp_profile"">{2}</a>", Request.ServerVariables("HTTP_HOST"), Request.ServerVariables("URL"), Resources.Resource.UnsubscribeFromEmails))

				SendEmail(ToAddress, "", "", FromAddress, Subject, EmailBody.ToString().Trim(), True)
			End If

		Next
	End Sub

#End Region

End Class
